<template>
  <div class="home-container">
    <el-row :gutter="20">
      <!-- 欢迎卡片 -->
      <el-col :span="24">
        <el-card class="welcome-card">
          <div class="welcome-content">
            <div class="welcome-text">
              <h1>欢迎回来，{{ authStore.userInfo?.username }}！</h1>
              <p>这是您的学习仪表盘，在这里您可以管理您的学习进度和内容。</p>
            </div>
            <div class="welcome-avatar">
              <el-avatar :size="80" :src="userAvatar">
                <el-icon><User /></el-icon>
              </el-avatar>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" style="margin-top: 20px;">
      <!-- 统计卡片 -->
      <el-col :xs="24" :sm="12" :md="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon color="#409eff"><User /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">1</div>
              <div class="stat-label">在线用户</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :sm="12" :md="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon color="#67c23a"><Document /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">5</div>
              <div class="stat-label">学习课程</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :sm="12" :md="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon color="#e6a23c"><Trophy /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">3</div>
              <div class="stat-label">完成项目</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :sm="12" :md="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon color="#f56c6c"><Clock /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">24</div>
              <div class="stat-label">学习小时</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" style="margin-top: 20px;">
      <!-- 最近活动 -->
      <el-col :xs="24" :lg="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最近活动</span>
              <el-button type="text" size="small">查看更多</el-button>
            </div>
          </template>
          
          <el-timeline>
            <el-timeline-item timestamp="2024-01-15 10:30" placement="top">
              <el-card>
                <h4>完成 Bun 基础学习</h4>
                <p>学习了 Bun 的基本概念和安装配置</p>
              </el-card>
            </el-timeline-item>
            
            <el-timeline-item timestamp="2024-01-14 15:20" placement="top">
              <el-card>
                <h4>创建第一个 API 项目</h4>
                <p>使用 Bun 创建了 HTTP 服务器和认证系统</p>
              </el-card>
            </el-timeline-item>
            
            <el-timeline-item timestamp="2024-01-13 09:15" placement="top">
              <el-card>
                <h4>开始学习 Vue 3</h4>
                <p>学习了 Vue 3 的组合式 API 和响应式系统</p>
              </el-card>
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
      
      <!-- 快速操作 -->
      <el-col :xs="24" :lg="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>快速操作</span>
            </div>
          </template>
          
          <div class="quick-actions">
            <el-button type="primary" size="large" @click="goToProfile">
              <el-icon><User /></el-icon>
              个人中心
            </el-button>
            
            <el-button type="success" size="large" @click="goToUsers" v-if="isAdmin">
              <el-icon><UserFilled /></el-icon>
              用户管理
            </el-button>
            
            <el-button type="info" size="large" @click="showSettings">
              <el-icon><Setting /></el-icon>
              系统设置
            </el-button>
            
            <el-button type="warning" size="large" @click="showHelp">
              <el-icon><QuestionFilled /></el-icon>
              帮助中心
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  User,
  Document,
  Trophy,
  Clock,
  UserFilled,
  Setting,
  QuestionFilled
} from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

// 用户头像
const userAvatar = computed(() => {
  return authStore.userInfo?.avatar || ''
})

// 是否为管理员
const isAdmin = computed(() => {
  return authStore.userInfo?.username === 'admin'
})

// 跳转到个人中心
const goToProfile = () => {
  router.push('/dashboard/profile')
}

// 跳转到用户管理
const goToUsers = () => {
  router.push('/dashboard/users')
}

// 显示设置
const showSettings = () => {
  ElMessage.info('设置功能开发中...')
}

// 显示帮助
const showHelp = () => {
  ElMessage.info('帮助中心开发中...')
}
</script>

<style scoped>
.home-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0;
}

.welcome-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
  overflow: hidden;
  position: relative;
}

.welcome-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="dots" width="20" height="20" patternUnits="userSpaceOnUse"><circle cx="10" cy="10" r="1" fill="rgba(255,255,255,0.1)"/></pattern></defs><rect width="100" height="100" fill="url(%23dots)"/></svg>');
  opacity: 0.5;
}

.welcome-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  z-index: 1;
  padding: 40px;
}

.welcome-text h1 {
  margin: 0 0 16px 0;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.5px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.welcome-text p {
  margin: 0;
  opacity: 0.95;
  font-size: 18px;
  line-height: 1.6;
  font-weight: 400;
}

.welcome-avatar {
  margin-left: 32px;
}

.welcome-avatar .el-avatar {
  border: 3px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.stat-card {
  margin-bottom: 24px;
  border-radius: 12px;
  border: 1px solid #e4e7ed;
  transition: all 0.3s;
  overflow: hidden;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  border-color: #409eff;
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 24px;
}

.stat-icon {
  font-size: 40px;
  margin-right: 20px;
  padding: 12px;
  border-radius: 12px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7ed 100%);
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 8px;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}

.quick-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 20px;
  padding: 20px 0;
}

.quick-actions .el-button {
  height: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  font-weight: 600;
  transition: all 0.3s;
  border: 2px solid transparent;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  color: #495057;
}

.quick-actions .el-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  border-color: #409eff;
  background: linear-gradient(135deg, #409eff 0%, #667eea 100%);
  color: white;
}

.quick-actions .el-button .el-icon {
  font-size: 24px;
  margin-bottom: 8px;
}

:deep(.el-timeline-item__content .el-card) {
  margin: 0;
}

:deep(.el-timeline-item__content .el-card h4) {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #333;
}

:deep(.el-timeline-item__content .el-card p) {
  margin: 0;
  color: #666;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .welcome-content {
    flex-direction: column;
    text-align: center;
  }
  
  .welcome-avatar {
    margin: 20px 0 0 0;
  }
  
  .quick-actions {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
